//
//  SNOrderFormStateDetailCell.m
//  app
//
//  Created by 王少宁 on 15/5/6.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//  订单详情的 headerView

#import "SNOrderFormHeaderView.h"
#import "WXApi.h"

@interface SNOrderFormHeaderView ()
#pragma mark - 没有支付
@property (nonatomic, retain) UILabel * snLabel;
@property (nonatomic, retain) UILabel * moneyLabel;
@property (nonatomic, retain) UIButton * WXPayBtn;
@property (nonatomic, retain) UIButton * ZFBPayBtn;

#pragma mark - 已经支付
@property (nonatomic, retain) UIImageView * statusImgView;
@property (nonatomic, retain) UILabel * statusLabel;
@end

@implementation SNOrderFormHeaderView

-(void)setStyle:(OrderFormHeaderViewStyle)style
{
    if (style == 0) {
        [self createNonPayView];
    } else {
        [self createOtherViewWithStyle:style];
    }
    _style = style;
}


- (void)createNonPayView
{
    _snLabel = CREATE_WITH_FRAME_OF(UILabel, CGPointMake(0, 25),
                                    [self.ubo ow:SCREEN_WIDTH oh:20]);
    _snLabel.font = JKAppGlobleFontSmallTagText;
    _snLabel.textColor = [XCommon hexStringToColor:kColor_l_light_gray_text];
    _snLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_snLabel];
    
    _moneyLabel = CREATE_WITH_FRAME_OF(UILabel, [_snLabel.ufo fx:0 fy:1.0 oy:2],
                                       [_snLabel.ubo fw:1.0 fh:1.0]);
    _moneyLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_moneyLabel];
    
    CGFloat btnSpace = 30;
    CGFloat btnWidth = (SCREEN_WIDTH-30*3)*0.5;
    CGFloat btnHeitht = 45;
    
    if (![WXApi isWXAppInstalled]) {     //没有安装微信则不显示微信支付按钮
        _ZFBPayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _ZFBPayBtn.frame = CGRectOf([_moneyLabel.ufo ox:btnSpace fy:1.0 oy:20],
                                    [_moneyLabel.ubo ow:SCREEN_WIDTH-2*btnSpace oh:btnHeitht]);
        UIImage * ZFBBackGroundImg = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"zfbBackGround@2x" ofType:@"png"]];
        ZFBBackGroundImg = [ZFBBackGroundImg resizableImageWithCapInsets:UIEdgeInsetsMake(UP(6), UP(6), UP(6), UP(6)) resizingMode:UIImageResizingModeStretch];
        [_ZFBPayBtn setBackgroundImage:ZFBBackGroundImg forState:UIControlStateNormal];
        [_ZFBPayBtn addTarget:self action:@selector(ZFBPayBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_ZFBPayBtn];
        
        UILabel * zfbBtnLabel = [[UILabel alloc] init];
        zfbBtnLabel.textAlignment = NSTextAlignmentCenter;
        zfbBtnLabel.font = JKAppGlobleFontContentText;
        zfbBtnLabel.textColor = [UIColor whiteColor];
        zfbBtnLabel.center = _ZFBPayBtn.center;
        zfbBtnLabel.bounds = CGRectMake(0, 0, 100, btnHeitht);
        NSMutableAttributedString * zfbAttStr = [[NSMutableAttributedString alloc] init];
        NSTextAttachment *zfbAttachment = [[NSTextAttachment alloc] init];
        zfbAttachment.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"zfb@2x" ofType:@"png"]];
        zfbAttachment.bounds = CGRectMake(0, -5, 20, 20);
        NSAttributedString *zfbAttribStr = [NSAttributedString attributedStringWithAttachment:zfbAttachment];
        [zfbAttStr appendAttributedString:zfbAttribStr];
        [zfbAttStr appendAttributedString:[[NSAttributedString alloc] initWithString:@" 支付宝支付"]];
        zfbBtnLabel.attributedText = zfbAttStr;
        [self addSubview:zfbBtnLabel];
    } else {    //安装了微信则显示微信支付按钮
        _WXPayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _WXPayBtn.frame = CGRectOf([_moneyLabel.ufo ox:btnSpace fy:1.0 oy:20],
                                   [_moneyLabel.ubo ow:btnWidth oh:btnHeitht]);
        UIImage * wxBackgrounImg = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"wxPayBackGround@2x" ofType:@"png"]];
        wxBackgrounImg = [wxBackgrounImg resizableImageWithCapInsets:UIEdgeInsetsMake(UP(6), UP(6), UP(6), UP(6)) resizingMode:UIImageResizingModeStretch];
        [_WXPayBtn setBackgroundImage:wxBackgrounImg forState:UIControlStateNormal];
        [_WXPayBtn addTarget:self action:@selector(WXPayBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_WXPayBtn];
        
        UILabel * wxBtnLabel = [[UILabel alloc] init];
        wxBtnLabel.textAlignment = NSTextAlignmentCenter;
        wxBtnLabel.font = JKAppGlobleFontContentText;
        wxBtnLabel.textColor = [UIColor whiteColor];
        wxBtnLabel.center = _WXPayBtn.center;
        wxBtnLabel.bounds = CGRectMake(0, 0, 100, btnHeitht);
        NSMutableAttributedString * wxAttStr = [[NSMutableAttributedString alloc] init];
        NSTextAttachment *wxAttachment = [[NSTextAttachment alloc] init];
        wxAttachment.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"wxPay@2x" ofType:@"png"]];
        wxAttachment.bounds = CGRectMake(-3, -3, 18, 15.5);
        NSAttributedString *wxAttribStr = [NSAttributedString attributedStringWithAttachment:wxAttachment];
        [wxAttStr appendAttributedString:wxAttribStr];
        [wxAttStr appendAttributedString:[[NSAttributedString alloc] initWithString:@" 微信支付"]];
        wxBtnLabel.attributedText = wxAttStr;
        [self addSubview:wxBtnLabel];
        
        
        _ZFBPayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _ZFBPayBtn.frame = CGRectOf([_WXPayBtn.ufo fx:1.0 ox:btnSpace fy:0],
                                    [_WXPayBtn.ubo fw:1.0 fh:1.0]);
        UIImage * ZFBBackGroundImg = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"zfbBackGround@2x" ofType:@"png"]];
        ZFBBackGroundImg = [ZFBBackGroundImg resizableImageWithCapInsets:UIEdgeInsetsMake(UP(6), UP(6), UP(6), UP(6)) resizingMode:UIImageResizingModeStretch];
        [_ZFBPayBtn setBackgroundImage:ZFBBackGroundImg forState:UIControlStateNormal];
        [_ZFBPayBtn addTarget:self action:@selector(ZFBPayBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_ZFBPayBtn];
        
        UILabel * zfbBtnLabel = [[UILabel alloc] init];
        zfbBtnLabel.textAlignment = NSTextAlignmentCenter;
        zfbBtnLabel.font = JKAppGlobleFontContentText;
        zfbBtnLabel.textColor = [UIColor whiteColor];
        zfbBtnLabel.center = _ZFBPayBtn.center;
        zfbBtnLabel.bounds = CGRectMake(0, 0, 100, btnHeitht);
        NSMutableAttributedString * zfbAttStr = [[NSMutableAttributedString alloc] init];
        NSTextAttachment *zfbAttachment = [[NSTextAttachment alloc] init];
        zfbAttachment.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"zfb@2x" ofType:@"png"]];
        zfbAttachment.bounds = CGRectMake(0, -5, 20, 20);
        NSAttributedString *zfbAttribStr = [NSAttributedString attributedStringWithAttachment:zfbAttachment];
        [zfbAttStr appendAttributedString:zfbAttribStr];
        [zfbAttStr appendAttributedString:[[NSAttributedString alloc] initWithString:@" 支付宝支付"]];
        zfbBtnLabel.attributedText = zfbAttStr;
        [self addSubview:zfbBtnLabel];
    }
}

//点击了微信支付
- (void)WXPayBtnClick
{
    if ([_deletage respondsToSelector:@selector(wxPayBtnClick)]) {
        [_deletage performSelector:@selector(wxPayBtnClick)];
    }
}

//点击了支付宝支付
- (void)ZFBPayBtnClick
{
    if ([_deletage respondsToSelector:@selector(zfbPayBtnClick)]) {
        [_deletage performSelector:@selector(zfbPayBtnClick)];
    }
}

- (void)createOtherViewWithStyle:(OrderFormHeaderViewStyle)style
{
    _statusImgView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*0.5-32.5, 20, 65, 65)];
    [self addSubview:_statusImgView];
    
    _statusLabel = CREATE_WITH_FRAME_OF(UILabel, [_statusImgView.ufo fx:0 fy:1.0 oy:10],
                                        [_statusImgView.ubo fw:1.0 oh:20]);
    _statusLabel.textColor = [XCommon hexStringToColor:kColor_l_light_gray_text];
    _statusLabel.textAlignment = NSTextAlignmentCenter;
    _statusLabel.font = [UIFont boldSystemFontOfSize:15];
    [self addSubview:_statusLabel];
    
    _snLabel = CREATE_WITH_FRAME_OF(UILabel, [self.ubo fx:0 oy:_statusLabel.ufo.yy],
                                    [self.ubo ow:SCREEN_WIDTH oh:20]);
    _snLabel.textColor = [XCommon hexStringToColor:kColor_s_light_gray_text];
    _snLabel.font = JKAppGlobleFontSmallTagText;
    _snLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_snLabel];
}

- (void)viewWithModel:(SNOrderFormDetailModel *)model
{
    _snLabel.text = [NSString stringWithFormat:@"订单号 : %@",model.sn];
    
    if (self.style == 0) {
#pragma mark - 没有支付
        NSString * str = [NSString stringWithFormat:@"金额 :￥%@",model.money];
        NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc] initWithString:str];
        [attributedStr addAttributes:@{NSFontAttributeName:JKAppGlobleFontSmallTagText,NSForegroundColorAttributeName:[XCommon hexStringToColor:kColor_l_light_gray_text]} range:NSMakeRange(0, 4)];
        [attributedStr addAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:17]} range:NSMakeRange(4, str.length-4)];
        _moneyLabel.attributedText = attributedStr;
    } else {
        if ([model.isReturns intValue] == 0) {
            switch ([model.ostatus intValue]) {
                case 1:
                    _statusLabel.text = @"支付成功";
                    _statusImgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"success_orange@2x" ofType:@"png"]];
                    break;
                case 2:
                    _statusLabel.text = @"已发货";
                    _statusImgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"success_orange@2x" ofType:@"png"]];
                    break;
                case 4:
                    _statusLabel.text = @"已完成";
                    _statusImgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"success_orange@2x" ofType:@"png"]];
                    break;
                case 5:
                    _statusLabel.text = @"已失效";
                    _statusImgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cancle@2x" ofType:@"png"]];
                    break;
                case 6:
                    _statusLabel.text = @"订单取消";
                    _statusImgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cancle@2x" ofType:@"png"]];
                    break;
                case 8:
                    _statusLabel.text = @"备货中";
                    _statusImgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"success_orange@2x" ofType:@"png"]];
                    break;
                    
                case 9:
                    _statusLabel.text = @"备货中";
                    _statusImgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"success_orange@2x" ofType:@"png"]];
                    break;
                    
                default:
                    break;
            }
        } else {
            _statusLabel.text = @"售后中";
            _statusImgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"afterCost@2x" ofType:@"png"]];
        }
    }
}

- (void)drawRect:(CGRect)rect
{
    [self drawBackground:rect color:[UIColor whiteColor]];
}

@end
